package com.cn.kehong.service;


import com.cn.kehong.api.IProductPackageFenjiService;
import com.cn.kehong.dao.ProductPackageFenjiMapper;
import com.cn.kehong.dao.ProductPackageMapper;
import com.cn.kehong.domain.ProductPackage;
import com.cn.kehong.domain.ProductPackageExample;
import com.cn.kehong.domain.ProductPackageFenji;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductPackageFenjiServiceImpl implements IProductPackageFenjiService {

    @Autowired
    private ProductPackageFenjiMapper productPackageFenjiMapper;

    @Autowired
    private ProductPackageMapper productPackageMapper;

    @Override
    public List<ProductPackageFenji> getProductPackageFenjiByPackageId(String packageId) throws Exception {
        if(StringUtils.isEmpty(packageId)){
            return Lists.newArrayList();
        }
        return null;
    }

    @Override
    public String addBatchProductPackageFenji(List<ProductPackageFenji> productPackageFenjis) throws Exception{
        if(CollectionsUtil.isEmpty(productPackageFenjis)){
            return "数组为空";
        }
        for(ProductPackageFenji packageFenji:productPackageFenjis){
            packageFenji.setId(CommonUtil.generateRandomNum("pfenji-"));
        }
        int count = productPackageFenjiMapper.batchInsertPackageFenji(productPackageFenjis);
        if(count < 1){
            return "插入表异常";
        }

        ProductPackage update = new ProductPackage();
        update.setHasBind("1");
        ProductPackageExample packageExample = new ProductPackageExample();
        packageExample.createCriteria().andIdEqualTo(productPackageFenjis.get(0).getPackageId());
        count = productPackageMapper.updateByExampleSelective(update,packageExample);
        if(count < 1){
            throw new Exception("更新表异常！！");
        }
        return CommonUtil.outStr(count);
    }
}
